package com.ruoyi.tester.executor.expression;

import com.ruoyi.tester.executor.ExpressionContext;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

/**
 * @author SongJun
 * @description TODO:
 * @date 2022/4/27 11:08
 */
@Component
public class ExpMap implements ExpressionAnalysis {
    @Override
    public String getExpressionName() {
        return "map";
    }

    @Override
    public String getDescription() {
        return "map(key) 返回当前json类型的上下文中属性为key的value值;";
    }

    @Override
    public Object analysis(ExpressionContext context, String... param) {
        JSONObject node;
        if(context.getContext() instanceof JSONObject){
            node = (JSONObject) context.getContext();
        } else if(context.getContext() instanceof String){
            node = new JSONObject((String)context.getContext());
        } else {
            node = new JSONObject(context.getContext());
        }
        String jsonKey = param[0];
        return node.get(jsonKey);
    }
}
